package library;

import library.utils.Utils;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import library.model.BooksAuthorPojo;
import library.utils.ImageLoader;
import library.utils.ImagePanel;

/**
 * @author Dmitry
 */
public class BookDescriptionPanel extends JPanel {
    private BooksAuthorPojo data;
    //private JEditorPane html;
    JTextPane html;
    private JScrollPane scroll;
    private BufferedImage image;
    private ImageLoader loader;
    private ImagePanel imagePanel;
    private final JPanel buttonsPanel;
    private OnDownload event;
    private JButton download;

    public interface OnDownload {
        public void onDownload(BooksAuthorPojo toBeDownload);
    }

    public BookDescriptionPanel() {
        setLayout(new BorderLayout());
        html = new JTextPane();
        //html = new JEditorPane();
        html.setContentType("text/html");
        html.setEditable(false);
        html.setPreferredSize(new Dimension(500, 500));
        html.setMaximumSize(new Dimension(500, 10000));

        scroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scroll.getViewport().add(html);
        scroll.setBorder(BorderFactory.createEmptyBorder());
        add(scroll);
        // add(html);
        buttonsPanel = createButtonPanel();
        add(buttonsPanel, BorderLayout.NORTH);
        imagePanel = new ImagePanel();
    }

    private JPanel createButtonPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setOpaque(true);
        panel.setBackground(Color.WHITE);
        download = new JButton(Utils.loadIcon("download.png"));
        download.setEnabled(false);
        download.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (event != null && data != null) {
                    event.onDownload(data);
                }
            }
        });
        panel.add(download, BorderLayout.EAST);
        return panel;
    }

    public void setOnDownloadEvent(OnDownload event) {
        this.event = event;
    }

    public void setBookAuthorObject(BooksAuthorPojo data) {
        this.data = data;
        image = null;
        refresh();
        download.setEnabled(true);
        // downloadImage();
    }

    public void clear() {
        download.setEnabled(false);
        html.setText("");
        if (imagePanel.getParent() != null) {
            imagePanel.getParent().remove(imagePanel);
        }
        validate();
        repaint();
    }

    public void refresh() {
        clear();
        if (data != null && !data.getDescription().equals("0")) {
            String description = data.getDescription();
            String text = "<html>" + description + "</html>";
            html.setText(text);
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    scroll.getVerticalScrollBar().setValue(0);
                }
            });
        }
    }

    private void downloadImage() {
        if (loader != null) {
            loader.interrupt();
            loader = null;
        }

        loader = new ImageLoader("" + data.getId()) {
            @Override
            public void onImageLoaded(BufferedImage image) {
                BookDescriptionPanel.this.image = image;
                final String text = html.getText();
                html.setText("");
                imagePanel.setImage(image);
                buttonsPanel.add(imagePanel, BorderLayout.WEST);
                Component c = SwingUtilities.getRoot(BookDescriptionPanel.this);
                c.revalidate();
                c.validate();
                c.repaint();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        html.setText(text);
                    }
                });
            }
        };
        loader.start();
    }
}
